package training.bms.presentation;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import training.bms.business.Blog;
import training.bms.business.BlogController;
import training.bms.business.BlogSearchOptions;
import training.bms.business.Post;
import training.bms.business.PostController;
import training.bms.business.PostSearchOptions;

@ManagedBean
@SessionScoped
public class SearchPost {
	private static final int MAX_RESULTS = 10;
	
	private List<Blog> blogs;
	private PostSearchOptions options;
	private List<Integer> pages;
	private int page;
	private List<Post> result;
	private Post post;
	private boolean postSaved;
	
	public SearchPost() {
		initialize();
	}
	
	public String initialize() {
		blogs = searchBlogs();
		options = new PostSearchOptions();
		options.setMaxResults(MAX_RESULTS);
		pages = null;
		result = null;
		post = null;
		postSaved = false;
		return "searchPost";
	}
	
	private List<Blog> searchBlogs() {
		BlogSearchOptions options = new BlogSearchOptions();
		BlogController controller = new BlogController();
		return controller.searchEntity(options);
	}
	
	public List<Blog> getBlogs() {
		return blogs;
	}

	public void setOptions(PostSearchOptions options) {
		this.options = options;
	}	

	public PostSearchOptions getOptions() {
		return options;
	}
	
	public void setPages(List<Integer> pages) {
		this.pages = pages;
	}

	public List<Integer> getPages() {
		return pages;
	}
	
	public void setPage(int page) {
		this.page = page;
	}
	
	public int getPage() {
		return page;
	}	

	public void setResult(List<Post> result) {
		this.result = result;
	}	
	
	public List<Post> getResult() {
		return result;
	}

	public void setPost(Post post) {
		this.post = post;
	}

	public Post getPost() {
		return post;
	}

	public void setPostSaved(boolean postSaved) {		
		this.postSaved = postSaved;
	}
	
	public boolean getPostSaved() {
		return postSaved;
	}

	public void searchPost() {
		pages = new ArrayList<Integer>();
		PostController controller = new PostController();
		int postCount = controller.searchEntityCount(options);
		if (postCount == 0) {
			FacesMessage message = new FacesMessage();
			message.setSeverity(FacesMessage.SEVERITY_INFO);
			message.setSummary("Post not found");
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, message);
		} else {			
			int page = 1;
			while (postCount > 0) {
				pages.add(page);
				++page;
				postCount -= options.getMaxResults();
			}
			goToFirst();
		}
	}
	
	public void goToFirst() {
		goTo(1);
	}

	public void goToPrevious() {
		if (page > 1) {
			goTo(page - 1);
		}
	}	
	
	public void goTo(int page) {
		int firstResult = (page - 1) * options.getMaxResults();
		options.setFirstResult(firstResult);
		PostController controller = new PostController();
		result = controller.searchEntity(options);
		this.page = page;
	}
	
	public void goToNext() {
		if (page < pages.size()) {
			goTo(page + 1);
		}
	}
	
	public void goToLast() {
		goTo(pages.size());
	}		
	
	public String editPost(Post post) {
		this.post = (Post) post.clone();
		this.postSaved = false;
		return "editPost";
	}
	
	public void savePost() {
		PostController controller = new PostController();
		controller.saveEntity(post);
		postSaved = true;
		FacesMessage message = new FacesMessage();
		message.setSummary("Post has been successfully saved");
		message.setSeverity(FacesMessage.SEVERITY_INFO);
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(null, message);					
	}
}
